package com.atguigu.job;

import com.atguigu.constant.MqConst;
import org.redisson.api.RBloomFilter;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;


//开启任务调度
@EnableScheduling
@Component
public class ScheduledJob {
    @Autowired
    private RabbitTemplate rabbitTemplate;
//    @Autowired
//    private SkuInfoMapper skuInfoMapper;
    @Autowired
    private RBloomFilter<Long> skuBloomFilter;

    //商品下架操作
    @Scheduled(cron = "0 0 3 * * ? ")
    public void taskEveryNight03() {
        //https://cron.qqe2.com/
        // 分，时，日，月，周，年
        rabbitTemplate.convertAndSend(MqConst.CLEAR_REDIS_EXCHANGE, MqConst.CLEAR_REDIS_ROUTE_KEY, "");
        System.out.println("开始下架商品");
    }

    //商品上架操作
    @Scheduled(cron = "0 0 5 * * ? ")
    public void taskEveryNight05() {
        rabbitTemplate.convertAndSend(MqConst.SCAN_SECKILL_EXCHANGE, MqConst.SCAN_SECKILL_ROUTE_KEY, "");
        System.out.println("开始上架商品");
    }

    //定时初始化-布隆过滤器
    @Scheduled(cron = "0 0 4 * * ? ")
    public void taskEveryNight04() {
        //重新初始化 把原来的删除掉
        skuBloomFilter.delete();
        skuBloomFilter.tryInit(10000000, 0.001);

        //a.查询指定表的数据库
//        List<SkuInfo> skuInfoList = skuInfoMapper.selectList(null);
//        for (SkuInfo skuInfo : skuInfoList) {
//            Long skuInfoId = skuInfo.getId();
//            skuBloomFilter.add(skuInfoId);
//        }
        System.out.println("开始布隆过滤器初始化");
    }
}
